package com.binhthuan.catechist.client.grid;

import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.ECIcons;
import com.binhthuan.catechist.client.component.PopupDialog;
import com.binhthuan.catechist.client.data.TrainerDS;
import com.binhthuan.catechist.client.viewer.TrainerViewer;
import com.binhthuan.catechist.shared.constant.Action;
import com.binhthuan.catechist.shared.constant.Parameter;
import com.google.gwt.user.client.ui.Anchor;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 * 
 * @author dttgiang Hiển thị danh sách trainer. Để lấy danh sách trainer theo
 *         điều kiện, tạo 1 lớp kế thừa từ lớp này và override hàm
 *         {@link com.binhthuan.catechist.client.grid.AbstractGrid#initializeCriteria()}
 * 
 */
public class TrainerGrid extends AbstractGrid {

    public TrainerGrid() {
    }

    public TrainerGrid(TrainerDS dataSource) {
        super(dataSource);

        ListGridField id = new ListGridField(Parameter.ID);
        id.setHidden(true);
        id.setCanHide(false);

        ListGridField active = new ListGridField(Parameter.ACTIVE);
        active.setTitle(" ");
        active.setType(ListGridFieldType.BOOLEAN);
        active.setWidth(24);

        ListGridField patronSaint = new ListGridField(Parameter.PATRON_SAINT);
        patronSaint.setTitle(ECApplication.getConstants().patronSaint());
        patronSaint.setWidth(70);

        ListGridField lastName = new ListGridField(Parameter.LAST_NAME);
        lastName.setTitle(ECApplication.getConstants().lastName());

        ListGridField firstName = new ListGridField(Parameter.FIRST_NAME);
        firstName.setTitle(ECApplication.getConstants().firstName());

        ListGridField birthday = new ListGridField(Parameter.BIRTHDAY);
        birthday.setTitle(ECApplication.getConstants().birthday());

        ListGridField gender = new ListGridField(Parameter.MALE);
        gender.setTitle(" ");
        gender.setCellFormatter(new CellFormatter() {

            @Override
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                boolean isMale = (value == null) ? false : Boolean.valueOf(value.toString());
                return Canvas.imgHTML(isMale ? ECIcons.getIcon("male.png") : ECIcons.getIcon("female.png"), 16, 16);
            }
        });
        gender.setWidth(20);

        ListGridField phone = new ListGridField(Parameter.PHONE);
        phone.setTitle(ECApplication.getConstants().phone());

        ListGridField email = new ListGridField(Parameter.EMAIL);
        email.setTitle(ECApplication.getConstants().email());
        email.setCellFormatter(new CellFormatter() {

            @Override
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                
                if (value != null) {
                    Anchor link = new Anchor(value.toString(), true);
                    link.setHref("mailto:" + value);
                    return link.toString();
                }
                return null;
            }
        });

        setFields(id, active, patronSaint, lastName, firstName, gender, birthday, phone, email);
        setBooleanTrueImage(ECIcons.getIcon("active.png"));
        setBooleanFalseImage(ECIcons.getIcon("inactive.png"));
    }

    /* (non-Javadoc)
     * @see com.binhthuan.catechist.client.grid.AbstractGrid#viewRecordClick(com.smartgwt.client.widgets.grid.ListGridRecord)
     */
    @Override
    protected void onViewClick(ListGridRecord selectedRecord) {
        TrainerViewer viewer = new TrainerViewer(TrainerDS.getInstance());
        viewer.fetchData(new Criteria(Parameter.ID, selectedRecord.getAttribute(Parameter.ID)));
        PopupDialog dialog = new PopupDialog(true);
        dialog.addItem(viewer.createCanvas());
        dialog.show();
    }

    /* (non-Javadoc)
     * @see com.binhthuan.catechist.client.grid.AbstractGrid#initializeCriteria()
     */
    @Override
    protected Criteria initializeCriteria() {
        return new Criteria(Parameter.OPERATION_INFO, Action.GET_ALL_TRAINERS);
    }
}
